package com.yuyou.train.user.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.yuyou.train.service.api.user.entity.UserDataConfig;
import com.yuyou.train.user.mapper.UserDataConfigMapper;
import com.yuyou.train.user.service.IUserDataConfigService;
import com.yuyou.train.util.enums.user.UserEnums;
import com.yuyou.train.util.message.lesson.LessonMess;
import com.yuyou.train.util.tool.Sequence;
import com.yuyou.train.util.tool.UUIDUtil;

@Service
public class UserDataConfigServiceImpl implements IUserDataConfigService{

	@Autowired
	private UserDataConfigMapper userDataConfigMapper;
	
	
	@Override
	public int deleteByPrimaryKey(String id) {
		return userDataConfigMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int deleteUserDataConfig(String userId, String type) {
		return userDataConfigMapper.deleteUserDataConfig(userId, type);
	}

	@Override
	public int insert(UserDataConfig record) {
		record.setId(Sequence.getSequence().getNextId());
		record.setCreateTime(new Date());
		return userDataConfigMapper.insert(record);
	}

	@Override
	public UserDataConfig selectByPrimaryKey(String id) {
		return userDataConfigMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(UserDataConfig record) {
		return userDataConfigMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int batchInsertUserDataConfig(List<UserDataConfig> userDataConfigList){
		return userDataConfigMapper.batchInsertUserDataConfig(userDataConfigList);
	}

	@Override
	public List<String> findDataIdByUserIdAndType(String userId, String type) {
		return userDataConfigMapper.findDataIdByUserIdAndType(userId, type);
	}

	@Override
	public List<String> findDataNameByUserIdAndType(String userId, String type) {
		return userDataConfigMapper.findDataNameByUserIdAndType(userId, type);
	}

	@Override
	public String selectUserSubjectId(String userId) throws Exception {
		List<String> dataIds=this.findDataIdByUserIdAndType(userId, UserEnums.userDataConfigType.subject.getValue());
		if(CollectionUtils.isEmpty(dataIds)){
			throw new Exception(LessonMess.ERR_EMPTY_SUBJECT);
		}
		return dataIds.get(0);
	}

	@Override
	public List<String> selectTeacherSubjectId(String userId) throws Exception{
		List<String> dataIds=this.findDataIdByUserIdAndType(userId, UserEnums.userDataConfigType.subject.getValue());
		if(CollectionUtils.isEmpty(dataIds)){
			throw new Exception(LessonMess.ERR_EMPTY_SUBJECT);
		}
		
		return dataIds;
	}
}